/*
 * Copyright 2016, The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.tencent.cloud.iov.util;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.FileProvider;

import java.io.File;


/**
 * Activity 工具类
 *
 * @author showxiao
 * @date 2018/3/27
 */

public class ActivityUtils {

    /**
     * 添加 Fragment 至 Activity
     *
     * @param fragmentManager 目标 Activity 对应 FragmentManager
     * @param fragment        待添加的 fragment
     * @param frameId         fragment 容器 ID
     */
    public static void addFragmentToActivity(@NonNull FragmentManager fragmentManager,
                                             @NonNull Fragment fragment, int frameId) {
        CommonUtils.checkNotNull(fragmentManager);
        CommonUtils.checkNotNull(fragment);
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.add(frameId, fragment);
        transaction.commit();
    }

    public static void startActivity(Activity activity, Intent intent) {
        activity.startActivity(intent);
    }

    public static void startActivityForResult(Activity activity, Intent intent) {
        activity.startActivityForResult(intent, 0);
    }

    public static void startActivityForResult(Activity activity, Intent intent, int requestCode) {
        activity.startActivityForResult(intent, requestCode);
    }

    public static void backToPreActivity(Activity activity, Intent intent) {
        activity.startActivity(intent);
    }

    public static void openAlbum(Activity activity, int requestCode) {
        Intent intent = new Intent(Intent.ACTION_PICK, null);
        intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
        startActivityForResult(activity, intent, requestCode);
    }

    public static void openCamera(Activity activity, int requestCode, String imageFileName) {
        //创建一个file，用来存储拍照后的照片
        File outputFile = new File(activity.getExternalCacheDir(), imageFileName);
        try {
            if (outputFile.exists()) {
                outputFile.delete();//删除
            }
            outputFile.createNewFile();
        } catch (Exception e) {
            e.printStackTrace();
        }
        Uri imageUri;
        if (Build.VERSION.SDK_INT >= 24) {
            imageUri = FileProvider.getUriForFile(activity,
                    "com.tencent.cloud.iov.util.provider",
                    outputFile);
        } else {
            imageUri = Uri.fromFile(outputFile);
        }
        //启动相机程序
        Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
        intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
        startActivityForResult(activity, intent, requestCode);
    }

}
